﻿using System;
using System.Collections.Generic;
using System.Text;
using BlogManager;
using System.Xml;
 

namespace QQ.Util
{
    static class ConfigXML
    {
        static private System.Xml.XmlDocument xml;
        static private readonly string xmlFile = Environment.CurrentDirectory + "\\" + "config.xml";
        static private string RootNodeName = "Config"; 

  
         
        public static List<Blog> GetValues(string name)
        {
            List<Blog> list = new List<Blog>();
            string blogTitle = "";
            string blogUrl = "";
            string blogUsername = "";
            string blogPassword = "";
            string blogType = "";
            string blogDesc = "";
            string blogKeyword = "";

            if (xml == null)
            {
                Load();
            }
            System.Xml.XmlNode node = xml.DocumentElement.SelectSingleNode(name);
            if (node != null)
            {
                foreach (System.Xml.XmlNode childNode in node.ChildNodes)
                {
                    if (childNode != null && childNode.InnerText != "")
                    {
                        try
                        {
                           // list.Add(new KeyValuePair<string, object>(childNode.Name, childNode.InnerText));
                           
                            foreach (System.Xml.XmlNode childNode2 in childNode)
                            {

                                
                                //将信息保存至集合
                               
                                switch (childNode2.Name)
                                {
                                    case "BlogTitle":
                                         blogTitle = childNode2.InnerText;
                                        break;
                                    case "BlogUrl":
                                         blogUrl = childNode2.InnerText;
                                        break;
                                    case "BlogType":
                                         blogType = childNode2.InnerText;
                                        break;
                                    case "BlogUserName":
                                        blogUsername = childNode2.InnerText;
                                        break;
                                    case "BlogPassWord":
                                        blogPassword = childNode2.InnerText;
                                        break;
                                    case "BlogKeyword":
                                        blogKeyword = childNode2.InnerText;
                                        break;
                                    case "BlogDesc":
                                        blogDesc = childNode2.InnerText;
                                        break;   
                                    default:
                                        break;
                                } 
                            }

                            Blog b = new Blog();
                            b.blogPassowod = blogPassword;
                            b.blogTitle = blogTitle;
                            b.blogType = blogType;
                            b.blogUrl = blogUrl;
                            b.blogUsername = blogUsername;
                            b.blogDesc = blogDesc;
                            b.blogKeyword = blogKeyword;
                            list.Add(b);
                           
                        }
                        catch { }
                    }
                }
            }
            return list;
        }

        public static bool SetValue(string name, List<KeyValuePair<string, object>> value)
        {
            if (xml == null)
            {
                Load();
            }
            try
            {
                System.Xml.XmlNode node = xml.DocumentElement.SelectSingleNode(name);
                if (node != null)
                {
                    node.RemoveAll();
                }
                else
                {
                    System.Xml.XmlNode root = xml.SelectSingleNode(RootNodeName);
                    node = xml.CreateElement(name);
                    root.AppendChild(node);
                }
                foreach (KeyValuePair<string, object> pari in value)
                {
                    System.Xml.XmlElement xe = xml.CreateElement(pari.Key);
                    xe.InnerText = pari.Value != null ? pari.Value.ToString() : "";
                    node.AppendChild(xe);
                }
                return true;
            }
            catch { return false; }
        }


        public static bool Save()
        {
            if (xml == null)
            {
                return false;
            }
            try
            {
                xml.Save(xmlFile); return true;
            }
            catch { }
            return false;
        }

        public static List<Blog> Content
        {
            get
            {
                List<Blog> list =  GetValues("Content"); 
                return list;
            }
            set
            {

                SetValue("Content", value);
                xml.Save(xmlFile);
            }
        }

         

        
        private static bool Load()
        {
            try
            {
                if (xml == null)
                {
                    if (!System.IO.File.Exists(xmlFile))
                    {
                        System.IO.FileStream stream = System.IO.File.Create(xmlFile);
                        stream.Close();
                    }
                    xml = new System.Xml.XmlDocument();
                    xml.Load(xmlFile);
                }
                return true;
            }
            catch (System.Xml.XmlException)
            {
                 try
                {
                   // string str = global::BlogManager.Properties.Resources.config;
                   // xml.LoadXml(str);
                   // xml.Save(xmlFile);
                    return false;
                }
                catch { return false; } 
            }
            catch
            {
                return false;
            }
        }

        public static bool IsLoad()
        {
            return Load();
        }

        public static string GetValue(string name)
        {
            try
            {
                if (xml == null)
                {
                    Load();
                }
                string type = xml.DocumentElement.SelectSingleNode(name).InnerText;
                return type;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static bool SetValue(string name, string value, bool aotoSave)
        {
            if (xml == null)
            {
                Load();
            }
            System.Xml.XmlNode node = xml.DocumentElement.SelectSingleNode(name);
            try
            {
                if (node == null)
                {
                    System.Xml.XmlNode root = xml.SelectSingleNode(RootNodeName);
                    System.Xml.XmlElement xe = xml.CreateElement(name);
                    xe.InnerText = value;
                    root.AppendChild(xe);
                }
                else
                {
                    node.InnerText = value;
                }
                if (aotoSave)
                {
                    xml.Save(xmlFile);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool SetValue(string name, string value)
        {
            return SetValue(name, value, true);
        }


        public static bool SetValue(string name,   List<Blog> blist)
        {
            if (xml == null)
            {
                Load();
            }
            try
            {
                System.Xml.XmlNode node = xml.DocumentElement.SelectSingleNode(name);
                if (node != null)
                {
                    node.RemoveAll();
                }
                else
                {
                    System.Xml.XmlNode root = xml.SelectSingleNode(RootNodeName);
                    node = xml.CreateElement(name);
                    root.AppendChild(node);
                }
                foreach (Blog b in blist)
                {
                    System.Xml.XmlElement xe = xml.CreateElement("Blog");
                    //=======================================
                    XmlNode blogTitle = xml.CreateElement("BlogTitle");
                    blogTitle.InnerText = b.blogTitle;
                    xe.AppendChild(blogTitle);
                    //========================================

                   //=======================================
                    XmlNode blogUrl = xml.CreateElement("BlogUrl");
                   blogUrl.InnerText = b.blogUrl;
                   xe.AppendChild(blogUrl);
                   //========================================

                   //=======================================
                   XmlNode blogType = xml.CreateElement("BlogType");
                   blogType.InnerText = b.blogType;
                   xe.AppendChild(blogType);
                   //========================================


                   //=======================================
                   XmlNode blogUsername = xml.CreateElement("BlogUserName");
                   blogUsername.InnerText = b.blogUsername;
                   xe.AppendChild(blogUsername);
                   //========================================



                   //=======================================
                   XmlNode blogPassword = xml.CreateElement("BlogPassWord");
                   blogPassword.InnerText = b.blogPassowod;
                   xe.AppendChild(blogPassword);
                   //========================================


                   //=======================================
                   XmlNode blogDesc = xml.CreateElement("BlogDesc");
                   blogDesc.InnerText = b.blogDesc;
                   xe.AppendChild(blogDesc);
                   //========================================


                   //=======================================
                   XmlNode blogKeyword = xml.CreateElement("BlogKeyword");
                   blogKeyword.InnerText = b.blogKeyword;
                   xe.AppendChild(blogKeyword);
                   //======================================== 
                    
                    node.AppendChild(xe);
                }
                return true;
            }
            catch { return false; }
        }


    }
}

 